define(function (require, exports, module) {
    var angular = require('angular');
    var dialog = require('@yunke/art-dialog').default;
    var util = require('@yunke/core/util').default;
    var ui = require("@yunke/core/ui");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀
    require("@yunke/jquery_pagination");
    require('angular-ui');
    require("uploader");
    require('../common/nav');

    angular.module('Balance', ['ngUI', 'myNav'])
        .controller('balanceListCtrl',['$scope', 'appservice', function ($scope, appservice) {
            appservice.name = '明源云客-合同结算';
            //合同列表
            $scope.contracts = [];

            //筛选列表
            $scope.filterDataStatus = [{
                name: '全部', selected: true, value: ''
            },  {
                name: '已返款', value: '1'
            }, {
                name: '未返款', value: '0'
            }];
            $scope.filterDataNature = [{
                name: '全部', selected: true, value: ''
            }];
            $scope.filterDataCities = [{
                name: '全部', selected: true, value: ''
            }];
            $scope.getFilterDataNature = function(){
                var url = api_prefix + "contract/get-contract-natures";

                $.ajax({
                    url: url,
                    type: 'get',
                    dataType: 'json',
                    success: function(res){
                        if(res.retCode === "0"){
                            var items = res.data;
                            var tabData = [{value: '', name: '全部', selected: true}];
                            var item;
                            for (var i = 0; item = items[i]; i++) {
                                tabData.push({
                                    value: item.property_id + '',
                                    name: item.property_name
                                });
                            }
                            $scope.filterDataNature = tabData;
                            $scope.$applyAsync();
                        }else{
                            util.alert(res.errMsg);
                        }
                    },
                    error: function(){
                        util.alert('系统维护，请稍候重试');
                    }
                })
            };
            $scope.getFilterDataCities = function(){
                var url = api_prefix + "contract/get-contract-area-list";

                $.ajax({
                    url: url,
                    type: 'get',
                    dataType: 'json',
                    success: function(res){
                        if(res.retCode === "0"){
                            var items = res.data;
                            var tabData = [{value: '', name: '全部', selected: true}];
                            var item;
                            for (var i = 0; item = items[i]; i++) {
                                tabData.push({
                                    value: item.areaId + '',
                                    name: item.areaName
                                });
                            }
                            $scope.filterDataCities = tabData;
                            $scope.$applyAsync();
                        }else{
                            util.alert(res.errMsg);
                        }
                    },
                    error: function(res){
                        util.alert('系统维护，请稍候重试');
                    }
                })
            };

            $scope.changeStatus = function (status) {
                $scope.query.execute();
            };
            $scope.changeNature = function (nature) {
                $scope.query.execute();
            };
            $scope.changeCity = function (city) {
                $scope.query.execute();
            };

            /**
             * 判断查询时间
             * @param a
             * @param b
             * @returns {boolean}
             */
            var compareDate = function(a,b){
                var arrA = a.split("-");
                var start = new Date(arrA[0],arrA[1],arrA[2]);
                var startTime = start.getTime();

                var arrB = b.split("-");
                var end = new Date(arrB[0],arrB[1],arrB[2]);
                var endTime = end.getTime();

                if(startTime > endTime){
                    return true
                }else{
                    return false
                }
            };

            /**
             * 查询条件
             * @type {{keyword: string, beginDate: string, endDate: string, execute: Function}}
             */
            $scope.query = {
                keyword:'',
                beginDate: '',
                endDate: '',
                execute:function(page){
                    var pageInx = page || 1;
                    var parms = {
                        keyword: this.keyword,
                        beginDate: this.beginDate,
                        endDate: this.endDate,
                        pageIndex: pageInx,
                        pageSize: 10
                    };

                    if(compareDate(this.beginDate,this.endDate)){
                        util.alert('开始时间不得大于结束时间');
                        return
                    }

                    //收集过滤条件
                    var filterStatus = $.grep($scope.filterDataStatus, function(o, n){
                        return o.selected == true;
                    });
                    if(filterStatus.length){
                        parms.status = filterStatus[0].value;
                    }
                    var filterNature = $.grep($scope.filterDataNature, function(o, n){
                        return o.selected==true;
                    });
                    if(filterNature.length){
                        parms.property = filterNature[0].value;
                    }
                    var filterCities = $.grep($scope.filterDataCities, function(o, n){
                        return o.selected==true;
                    });
                    if(filterCities.length){
                        parms.areaId = filterCities[0].value;
                    }

                    $scope.exportData = parms;

                    var url = api_prefix + "contract/get-balance-contract-list";

                    $.ajax({
                        url: url,
                        type: "get",
                        dataType: "json",
                        data: parms,
                        success: function (res) {
                            if(res.retCode === "0"){
                                var data = res.data;
                                $scope.contracts = data.items;
                                $scope.total = data.total;
                                $scope.$applyAsync();
                                $scope.getPage(pageInx);
                            }else{
                                util.alert(res.errMsg);
                            }
                        },
                        error: function(){
                            util.alert('系统维护，请稍候重试');
                        }
                    });
                }
            };


            //翻页控件
            $scope.getPage = function(page){
                var pageInx = page || 1;
                $scope.startPageIndex = (pageInx - 1)*10;
                $('#pagination').pagination($scope.total, {
                    current_page: pageInx - 1,
                    num_edge_entries: 1, //边缘页数
                    num_display_entries: 4, //主体页数
                    callback: function (page_index, jq) {
                        $scope.startPageIndex = (page_index - 1)*10;
                        $scope.query.execute(page_index);
                    },
                    items_per_page: 10 //每页显示1项
                });
            };

            /**
             * 导入结算状态
             */
            $('#importBalance').fileupload({
                dataType: 'json',
                url: api_prefix + "contract/import-balance-contract-list-execl",
                acceptFileTypes: /(\.|\/)(xls?x)$/i,
                maxFileSize: 10000000,
                done: function (e, data) {
                    var res = data.result;
                    if(res.retCode == '0'){
                        util.alert('导入结算状态成功');
                        $scope.query.execute();
                    }else{
                        util.alert(res.errMsg);
                    }
                },
                fail: function(e, data) {
                    util.alert('系统维护，请稍候重试');
                }
            });

            /**
             * 导出
             */
            $scope.exportData = {
                areaId: '',
                beginDate: '',
                endDate: '',
                keyword: '',
                pageIndex: 1,
                pageSize: 10,
                property: '',
                status: ''
            };
            $scope.export = function(){
                util.confirm('确定要导出数据吗？',{
                    ok:function(){
                        var url = api_prefix + "contract/export-balance-contract-list-execl";
                        var data = $scope.exportData

                        util.download(url,data);
                    }
                });
            };

            /**
             * 页面初始化
             */
            $scope.init = function(){
                $scope.getFilterDataNature();
                $scope.getFilterDataCities();
                $scope.query.execute();
            };
            $scope.init();
        }]);
});